package util;

import java.awt.Point;
import java.awt.geom.Point2D;
import java.io.Serializable;

/**
 * Implements a 2-dimensional point with double precision coordinates.
 */
public class astPoint implements Serializable, Cloneable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Holds the x- and y-coordinates of the point. Default is 0.
	 */
	protected double x, y;

	/**
	 * Constructs a new point at (0, 0).
	 */
	public astPoint() {
		this(0, 0);
	}

	/**
	 * Constructs a new point at the location of the given point.
	 * 
	 * @param point
	 *            Point that specifies the location.
	 */
	public astPoint(Point2D point) {
		this(point.getX(), point.getY());
	}

	/**
	 * Constructs a new point at the location of the given point.
	 * 
	 * @param point
	 *            Point that specifies the location.
	 */
	public astPoint(astPoint point) {
		this(point.getX(), point.getY());
	}

	/**
	 * Constructs a new point at (x, y).
	 * 
	 * @param x
	 *            X-coordinate of the point to be created.
	 * @param y
	 *            Y-coordinate of the point to be created.
	 */
	public astPoint(double x, double y) {
		setX(x);
		setY(y);
	}

	/**
	 * Returns the x-coordinate of the point.
	 * 
	 * @return Returns the x-coordinate.
	 */
	public double getX() {
		return x;
	}

	/**
	 * Sets the x-coordinate of the point.
	 * 
	 * @param value
	 *            Double that specifies the new x-coordinate.
	 */
	public void setX(double value) {
		x = value;
	}

	/**
	 * Returns the x-coordinate of the point.
	 * 
	 * @return Returns the x-coordinate.
	 */
	public double getY() {
		return y;
	}

	/**
	 * Sets the x-coordinate of the point.
	 * 
	 * @param value
	 *            Double that specifies the new x-coordinate.
	 */
	public void setY(double value) {
		y = value;
	}

	/**
	 * Returns the coordinates as a new point.
	 * 
	 * @return Returns a new point for the location.
	 */
	public Point getPoint() {
		return new Point((int) Math.round(x), (int) Math.round(y));
	}

	/**
	 * 
	 * Returns true if the given object equals this rectangle.
	 */
	public boolean equals(Object obj) {
		if (obj instanceof astPoint) {
			astPoint pt = (astPoint) obj;

			return pt.getX() == getX() && pt.getY() == getY();
		}

		return false;
	}

	/**
	 * Returns a new instance of the same point.
	 */
	public Object clone() {
		astPoint clone;

		try {
			clone = (astPoint) super.clone();
		} catch (CloneNotSupportedException e) {
			clone = new astPoint();
		}

		clone.setX(getX());
		clone.setY(getY());

		return clone;
	}

	public String toString() {
		return "mxPoint [" + x + "," + y + "]";
	}

}
